package com.qf.controller;

import com.qf.service.*;
import com.qf.util.DashboardUtils;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Cc
 * @version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/dashboard")
public class AdminDashbordController {
    private static final Logger logger = LoggerFactory.getLogger(AdminDashbordController.class);

    private static final int STATIS_DAYS_RANG = 31;// 统计的天数范围，一个月数据

    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    private DtsOrderService dtsOrderService;
    @Autowired
    private DtsCategoryService dtsCategoryService;

    /**
     * 首页展示  四个总数   用户 商品 货品 订单
     *
     * @return
     */
    @GetMapping("")
    public Object info() {
        HashMap<String, Object> map = new HashMap<>();
        int userTotal = dtsUserService.count();
        int goodsTotal = dtsGoodsService.count();
        int productTotal = dtsGoodsProductService.count();
        int orderTotal = dtsOrderService.count();

        map.put("orderTotal", orderTotal);
        map.put("productTotal", productTotal);
        map.put("goodsTotal", goodsTotal);
        map.put("userTotal", userTotal);
        logger.info("用户数量:{},商品数量:{},货品数量:{},订单数量:{}", userTotal, goodsTotal, productTotal, orderTotal);
        return ResponseUtil.ok(map);
    }


    /**
     * 首页数据可视化
     *
     * @return
     */
    @GetMapping("/chart")
    public Object chart() {
        //1.饼状图   商品分类  和对应分类的销售额
        CategorySellVo categorySellVo = new CategorySellVo();
        List<CategorySellAmts> categorySellAmtsList = dtsCategoryService.findGoodsCategoryAndTotal();
        categorySellVo.setCategorySellData(categorySellAmtsList);
        //商品分类的数组
        String[] categoryNames = categorySellAmtsList.stream()
                .map(CategorySellAmts::getName).collect(Collectors.toList())
                .toArray(new String[categorySellAmtsList.size()]);
        categorySellVo.setCategoryNames(categoryNames);

        //2日期和对应的订单总额, 订单笔数
        List<DayStatis> dayStatisList = dtsOrderService.findDayStatis();
        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(dayStatisList.stream().map(DayStatis::getDayStr).
                collect(Collectors.toList()).toArray(new String[dayStatisList.size()]));
        orderAmtsVo.setOrderCntData(dayStatisList.stream().map(DayStatis::getCnts).
                collect(Collectors.toList()).toArray(new Integer[dayStatisList.size()]));
        orderAmtsVo.setOrderAmtData(dayStatisList.stream().map(DayStatis::getAmts)
                .collect(Collectors.toList()));

        //3. 日期对应的用户数和订单数     统计用户的数量和时间  统计订单的数量和时间    订单的日期和数量使用 上个图的
        List<DayStatis> userCnts = dtsUserService.recentCount(STATIS_DAYS_RANG);
//        List<DayStatis> orderCnts = dtsOrderService.recentCount(STATIS_DAYS_RANG);
        UserOrderCntVo userOrderCnt = new UserOrderCntVo();

        //1.将两个统计时间数据合并去重
        String[] dayArray = DashboardUtils.unionStatisticDayStream(userCnts, dayStatisList);
        //2.根据用户的数量填充
        int[] userCounts = DashboardUtils.fillDataUserStatisticCnts(dayArray, userCnts);
        //3.根据日期填充订单的数据
        int[] orderCounts = DashboardUtils.fillDataUserStatisticCnts(dayArray, dayStatisList);

        userOrderCnt.setDayData(dayArray);
        userOrderCnt.setUserCnt(userCounts);
        userOrderCnt.setOrderCnt(orderCounts);

        HashMap map = new HashMap();
        map.put("categorySell", categorySellVo);
        map.put("orderAmts", orderAmtsVo);
        map.put("userOrderCnt", userOrderCnt);


        return ResponseUtil.ok(map);
    }

}
